/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package fmslauncher;

import java.io.File;
import java.io.IOException;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;
import javax.swing.JOptionPane;

/**
 *
 * @author rbs
 */
public class Main {

    /**
     * @param args the command line arguments
     * @throws InterruptedException
     * @throws IOException
     */
    public static void main(String[] args) throws InterruptedException, IOException {

        File folder = new File(getProgramPath() + "log");
        folder.mkdirs();
        
        FileHandler file = new FileHandler(getProgramPath() + "log" + File.separator + "fmslauncher.log", true);
        file.setFormatter(new SimpleFormatter());
        Logger.getLogger("").addHandler(file);
        Logger.getLogger("").setLevel(Level.ALL);
        Logger logger = Logger.getLogger(Main.class.toString());

        logger.log(Level.INFO, "Opened with {0} arguments...", args.length);
        Client client = null;
        
        try {
            client = new Client();
        } catch (Exception e) {
            logger.log(Level.SEVERE,"Konnte nicht zu Server verbinden!");
            //JOptionPane.showMessageDialog(null, "Konnte nicht zu Server verbinden!\n\r" + e);
            System.exit(3);
        }

        client.SendLine("CON FWDISP SERVER");
        if (client.RecLine().equals("CON OK"))
        {
            logger.log(Level.INFO, "Connection sucessful...sending data.");
            client.SendLine(args[0]);
            client.SendLine(args[1]);
            if(args.length == 3)
                client.SendLine(args[2]);
            else
                client.SendLine(" ");
        }
        else
            logger.log(Level.INFO, "not ready");
        Thread.sleep(20);
        client.Close();
    }
    
    public static String getProgramPath() {
        
        File file = new File(Main.class.getProtectionDomain().getCodeSource().getLocation().getFile());
        System.out.println("Path: "  +  file.getParent());
        return file.getParent() + File.separator;
    }

}
